package com.hanbang.taitaigou.ui.widget.banner;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;

import java.util.ArrayList;

public class CBLoopViewPager extends ViewPager {
    private CBPageAdapter mAdapter;
    private boolean isCanScroll = true;
    private boolean canLoop = true;
    private ArrayList<CircleDotsView> pointViews;
    private int[] pageIndicatorColor;

    public void setAdapter(PagerAdapter adapter, ArrayList<CircleDotsView> pointViews, int[] pageIndicatorColor, boolean canLoop) {
        mAdapter = (CBPageAdapter) adapter;
        this.canLoop = canLoop;
        /**
         *
         */
        mAdapter.setCanLoop(canLoop);
        mAdapter.setViewPager(this);
        this.pointViews = pointViews;
        this.pageIndicatorColor = pageIndicatorColor;
        super.setAdapter(mAdapter);
    }


    public boolean isCanScroll() {
        return isCanScroll;
    }

    public void setCanScroll(boolean isCanScroll) {
        this.isCanScroll = isCanScroll;
    }

    public CBPageAdapter getAdapter() {
        return mAdapter;
    }


    public CBLoopViewPager(Context context) {
        super(context);
        init();
    }

    public CBLoopViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        super.addOnPageChangeListener(onPageChangeListener);
    }

    private OnPageChangeListener onPageChangeListener = new OnPageChangeListener() {
        boolean isOk = false;

        @Override
        public void onPageSelected(int position) {
            setCurrentItem(position);
        }

        @Override
        public void onPageScrolled(int position, float positionOffset,
                                   int positionOffsetPixels) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    };

    public void setCurrentItem(int position) {
        setCurrentItem(position, false);
    }

    @Override
    public void setCurrentItem(int position, boolean smoothScroll) {
        int pointPosition = -1;
        if (isCanLoop()) {
            int viewSize = mAdapter.getCount();
            if (viewSize > 1) {
                if (position == viewSize - 1) {
                    pointPosition = 0;
                    super.setCurrentItem(1, smoothScroll);
                } else if (position == 0) {
                    pointPosition = viewSize - 3;
                    super.setCurrentItem(viewSize - 2, smoothScroll);
                } else if (position > 0) {
                    pointPosition = position - 1;

                    super.setCurrentItem(position, smoothScroll);
                }
            } else {
                pointPosition = 0;
                super.setCurrentItem(pointPosition, smoothScroll);
            }
        } else {
            pointPosition = position;
            super.setCurrentItem(pointPosition, smoothScroll);
        }
        if (pointPosition != -1) {
            // ((RadioButton) dotLayout.getChildAt(arg0 - 1)).setChecked(true);
            for (int i = 0; i < pointViews.size(); i++) {
                pointViews.get(i).setCircleColor(pageIndicatorColor[0]);
                if (pointPosition != i) {
                    pointViews.get(i).setCircleColor(pageIndicatorColor[1]);
                }
            }
        }
    }

    public boolean isCanLoop() {
        return canLoop;
    }

    public void setCanLoop(boolean canLoop) {
        this.canLoop = canLoop;
        if (mAdapter == null) return;
        mAdapter.setCanLoop(canLoop);
        mAdapter.notifyDataSetChanged();
    }

}
